import asyncio

import requests as requests
from pyppeteer import launch
from openpyxl import Workbook

async def main():
    # proxy = requests.get('http://proxy.jiayincredit.net/random').text  # 代理服务器地址和端口
    # print(f'proxy:{proxy}')
    # browser = await launch({'args': ['--proxy-server=http://' + proxy], 'headless': False})
    browser = await launch({'headless': False})
    page = await browser.newPage()
    await page.goto('http://www.dongchedi.com/auto/params-carIds-x-81')
    # await page.waitForNavigation()
    await asyncio.sleep(5)



    # allseries_ids = requests.get('https://www.dongchedi.com/motor/pc/car/brand/get_brand_series_list?aid=1839&app_name=auto_web_pc&brand_id=2&no_sales=0&show_historical_series=1&category_group_optimize=true').json()




    # 获取信息参数值
    # title = await page.evaluate('document.title')
    # print(f'title:{title}')
    await page.waitForSelector('#__next')
    elements = await page.xpath('//*[@id="__next"]/div/div/div/div[2]/div[2]/div/div/div[1]/label')
    values = []
    for i, element in enumerate(elements):
        value = await page.evaluate('(element) => element.textContent', element)
        values.append(value)
    print(values)  # 输出所有获取到的值



    #获取车型
    elements_chexing = await page.xpath('//*[@id="__next"]/div/div/div/div[2]/div[2]/div[1]/div[1]/div/div/h1/a')
    values_chexing = []
    for i, element_chexing in enumerate(elements_chexing):
        value = await page.evaluate('(element) => element.textContent', element_chexing)
        values_chexing.append(value)
    print(f'车型：{values_chexing}')  # 输出所有获取到的值

    #写入excel
    # book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    wb = Workbook()
    # sheet = book.add_sheet('懂车帝数据', cell_overwrite_ok=True)
    # 选择活动工作表
    ws = wb.active

    for i,chexing in enumerate(values_chexing):
        print(f'正在获取：{chexing}的配置')
        elements1 = await page.xpath(
            f'//*[@id="__next"]/div/div/div/div[2]/div[2]/div/div/div[1]/label/parent::div/parent::div/div[{i+2}]')
        values1 = []
        teshu = []
        values2 = ''
        for iss,element1 in enumerate(elements1):
            if iss in teshu and i > 0:
                elements12 = await page.xpath(
                    f'//*[@id="__next"]/div/div/div/div[2]/div[2]/div/div/div[1]/label/parent::div/parent::div/div[2]')
                childElement1222 = await elements12.querySelectorAll('div')
                values31 = []
                for isss1, ele1 in enumerate(childElement1222):
                    childElements1 = await ele1.querySelectorAll('div')
                    for isss22, ele22222 in enumerate(childElements1):
                        childElements2222 = await ele22222.querySelectorAll('div')
                        for isss333, ele33333 in enumerate(childElements2222):
                            value1 = await page.evaluate('(element) => element.textContent', ele33333)
                            values31.append(value1)
                N = len(values_chexing)
                new_lists = [values31[i2::N] for i2 in range(N)]
                new_lists1 = new_lists[i]
                values21 = ', '.join(new_lists1)
                values1.append(values21)
            else:
                childElement = await element1.querySelectorAll('div')

                if len(childElement) == 1:
                    value1 = await page.evaluate('(element) => element.textContent', childElement[0])
                    values2 = value1
                else:
                    if i == 0:
                        teshu.append(iss)
                    values3 = []
                    for isss, ele in enumerate(childElement):
                        childElements = await ele.querySelectorAll('div')
                        for isss22, ele2222 in enumerate(childElements):
                            childElements222 = await ele2222.querySelectorAll('div')
                            for isss33, ele3333 in enumerate(childElements222):
                                    value1 = await page.evaluate('(element) => element.textContent', ele3333)
                                    values3.append(value1)
                    N = len(values_chexing)
                    new_lists = [values3[i2::N] for i2 in range(N)]
                    new_lists1 =  new_lists[i]
                    values2 = ', '.join(new_lists1)
                values1.append(values2)
            print(f'values1:{values1}')  # 输出所有获取到的值
        for ioooo in range(0, len(values1)):
            # 渲染各个车型数据
            # sheet.write(i+1, ioooo + 1, values1[ioooo])
            ws.cell(row=i+2, column=ioooo + 2, value=values1[ioooo])
    #渲染表头列
    for i in range(0, len(values)):
        # sheet.write(0, i+1, values[i])
        ws.cell(row=1, column=i+2, value=values[i])

    #渲染车型
    col_chexing = tuple(values_chexing)
    for i in range(0, len(col_chexing)):
        # sheet.write(i+1, 0, col_chexing[i])
        ws.cell(row=i+2, column=1, value=col_chexing[i])


    # book.save('懂车帝奥迪A7.xlsx')
    wb.save('懂车帝奥迪s7.xlsx')




    # await page.screenshot({'path': 'example.png'})
    await browser.close()


if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())
